{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下载环境"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. graphrag环境"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pip install graphrag==1.2.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. vllm和fastchat环境"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pip install vllm accelerate\n",
    "\n",
    "pip install flash-attn --no-build-isolation \n",
    "\n",
    "pip install fschat==0.2.34\n",
    "\n",
    "pip install pydantic==2.11.4\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 启动vllm服务（需要使用支持openai接口的vllm服务）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python -m vllm.entrypoints.openai.api_server --served-model-name /root/autodl-tmp/Qwen/Qwen3-8B --model /root/autodl-tmp/Qwen/Qwen3-8B --dtype auto --port 8200 --max_model_len 8784 --gpu_memory_utilization 0.8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 启动fastchat服务"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 下载BAAI/bge-large-zh-v1.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#模型下载\n",
    "from modelscope import snapshot_download\n",
    "model_dir = snapshot_download('BAAI/bge-large-zh-v1.5',cache_dir='./')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 创建一个api.sh文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "latex"
    }
   },
   "outputs": [],
   "source": [
    "#!/bin/bash\n",
    "\n",
    "# 启动控制器\n",
    "python -m fastchat.serve.controller --host 0.0.0.0 --port 21003 &\n",
    "\n",
    "# 为模型工作进程设置环境变量并启动\n",
    "python -m fastchat.serve.model_worker --model-path /root/autodl-tmp/BAAI/bge-large-zh-v1.5 --model-names gpt-4 --num-gpus 1 --controller-address http://172.17.0.5:21003 --worker-address http://172.17.0.5:8080 --host 0.0.0.0 --port 8080 &\n",
    "\n",
    "# 启动openai API服务器\n",
    "python -m fastchat.serve.openai_api_server --controller-address http://172.17.0.5:21003 --host 0.0.0.0 --port 8300"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 启动fastchat服务"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "bash api.sh"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 如果出现错误：\n",
    "\n",
    "File \"/root/miniconda3/lib/python3.12/site-packages/pydantic/_migration.py\", line 296, in wrapper\n",
    "    raise PydanticImportError(\n",
    "pydantic.errors.PydanticImportError: `BaseSettings` has been moved to the `pydantic-settings` package. See https://docs.pydantic.dev/2.11/migration/#basesettings-has-moved-to-pydantic-settings for more details.\n",
    "\n",
    "## 解决方法：\n",
    "\n",
    "sudo vi sources.list\n",
    "\n",
    "复制以下内容到sources.list中\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty main multiverse restricted universe\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main multiverse restricted universe\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main multiverse restricted universe\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty-security main multiverse restricted universe\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main multiverse restricted universe\n",
    "\n",
    "sudo apt-get update\n",
    "sudo apt upgrade\n",
    "\n",
    "\n",
    "sudo apt-get install net-tools\n",
    "\n",
    "\n",
    "\n",
    "vim /root/miniconda3/lib/python3.12/site-packages/pydantic/_migration.py\n",
    "\n",
    "#from pydantic import BaseSettings \n",
    "\n",
    "from pydantic.v1.env_settings import BaseSettings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 配置GraphRAG环境"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 初始环境配置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "source /etc/network_turbo\n",
    "\n",
    "git clone https://github.com/microsoft/graphrag.git\n",
    "\n",
    "cd graphrag\n",
    "\n",
    "mkdir -p ./openl/input\n",
    "\n",
    "graphrag init  --root ./openl\n",
    "\n",
    "\n",
    "在 input文件夹下创建book.txt\n",
    "\n",
    "复制以下内容\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "健康生活的四个核心要素\n",
    "在现代快节奏生活中，保持健康需要从日常习惯入手。以下是经过科学验证的四大健康支柱：\n",
    "\n",
    "一、均衡饮食：给身体正确的燃料\n",
    "\n",
    "多样化摄入\n",
    "每天应摄入12种以上食物，每周达到25种。推荐\"彩虹饮食法\"：红色（番茄、草莓）含番茄红素；绿色（菠菜、西兰花）富含叶酸；紫色（蓝莓、紫甘蓝）含花青素。\n",
    "\n",
    "黄金比例搭配\n",
    "参考\"健康餐盘\"原则：\n",
    "\n",
    "50%非淀粉类蔬菜\n",
    "\n",
    "25%优质蛋白（鱼、豆类、瘦肉）\n",
    "\n",
    "25%全谷物（糙米、燕麦）\n",
    "\n",
    "加1份健康脂肪（坚果、橄榄油）\n",
    "\n",
    "需警惕的三大\"隐形杀手\"\n",
    "\n",
    "添加糖（每日≤25g）\n",
    "\n",
    "反式脂肪（常见于植脂末、代可可脂）\n",
    "\n",
    "高钠食品（方便面、腌制食品）\n",
    "\n",
    "二、科学运动：激活身体机能\n",
    "\n",
    "运动金字塔\n",
    "\n",
    "基础层：日常活动（步行、爬楼梯）\n",
    "\n",
    "中层：有氧运动（每周150分钟快走/游泳）\n",
    "\n",
    "高层：力量训练（每周2次）\n",
    "\n",
    "顶层：柔韧性练习（瑜伽、拉伸）\n",
    "\n",
    "办公室微运动\n",
    "每坐1小时进行3分钟运动：\n",
    "\n",
    "靠墙静蹲（强化膝关节）\n",
    "\n",
    "座椅抬臀（锻炼核心）\n",
    "\n",
    "手指对抗操（缓解鼠标手）\n",
    "\n",
    "警惕运动误区\n",
    "\n",
    "空腹运动易低血糖\n",
    "\n",
    "睡前3小时避免剧烈运动\n",
    "\n",
    "运动后及时补充电解质\n",
    "\n",
    "三、优质睡眠：身体的修复工程\n",
    "\n",
    "睡眠周期理论\n",
    "完整睡眠周期=90分钟（浅睡→深睡→REM睡眠），成年人需要4-6个周期。最佳入睡时间是22:00-23:00，此时褪黑素分泌高峰。\n",
    "\n",
    "睡眠优化方案\n",
    "\n",
    "创造黑暗环境（使用遮光窗帘）\n",
    "\n",
    "保持室温18-22℃\n",
    "\n",
    "睡前1小时避免蓝光（手机/电脑）\n",
    "\n",
    "尝试\"478呼吸法\"（吸气4秒→屏息7秒→呼气8秒）\n",
    "\n",
    "常见睡眠问题应对\n",
    "\n",
    "失眠：白天晒太阳30分钟\n",
    "\n",
    "早醒：检查皮质醇水平\n",
    "\n",
    "多梦：补充维生素B族\n",
    "\n",
    "四、心理平衡：被忽视的健康维度\n",
    "\n",
    "压力管理技巧\n",
    "\n",
    "正念冥想（每天10分钟）\n",
    "\n",
    "情绪日记（记录每日3件好事）\n",
    "\n",
    "自然疗法（森林浴、园艺）\n",
    "\n",
    "社交健康法则\n",
    "\n",
    "维护5-8个深度社交关系\n",
    "\n",
    "避免\"毒性关系\"（持续消耗能量的人际关系）\n",
    "\n",
    "每周至少1次面对面交流\n",
    "\n",
    "心理预警信号\n",
    "持续2周以上出现：\n",
    "\n",
    "兴趣减退\n",
    "\n",
    "睡眠食欲改变\n",
    "\n",
    "注意力分散\n",
    "需及时寻求专业帮助\n",
    "\n",
    "实践建议：21天习惯养成法\n",
    "第1-7天：记录现有习惯\n",
    "第8-14天：每天改变1个小行为\n",
    "第15-21天：巩固新习惯\n",
    "\n",
    "示例改善计划：\n",
    "\n",
    "晨起喝300ml温水\n",
    "\n",
    "午间散步15分钟\n",
    "\n",
    "晚餐减少精制碳水\n",
    "\n",
    "睡前进行10分钟冥想\n",
    "\n",
    "健康不是目标，而是生活方式。微小但持续的改善，比短期剧烈改变更有效。从今天开始，选择任何一个领域迈出第一步，身体会在未来感谢现在的你。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 配置settings.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "llm:\n",
    "  api_key: ${GRAPHRAG_API_KEY} # set this in the generated .env file\n",
    "  type: openai_chat # or azure_openai_chat\n",
    "  model: /root/autodl-tmp/Qwen/Qwen3-8B\n",
    "  model_supports_json: false # recommended if this is available for your model.\n",
    "  # audience: \"https://cognitiveservices.azure.com/.default\"\n",
    "  api_base: http://localhost:8200/v1\n",
    "  # api_version: 2024-02-15-preview\n",
    "  # organization: <organization_id>\n",
    "  # deployment_name: <azure_model_deployment_name>\n",
    "\n",
    "parallelization:\n",
    "  stagger: 0.3\n",
    "  # num_threads: 50\n",
    "\n",
    "async_mode: threaded # or asyncio\n",
    "\n",
    "embeddings:\n",
    "  async_mode: threaded # or asyncio\n",
    "  vector_store: \n",
    "    type: lancedb\n",
    "    db_uri: 'output/lancedb'\n",
    "    container_name: default\n",
    "    overwrite: true\n",
    "  llm:\n",
    "    api_key: ${GRAPHRAG_API_KEY}\n",
    "    type: openai_embedding # or azure_openai_embedding\n",
    "    model: gpt-4\n",
    "    api_base: http://localhost:8300/v1\n",
    "    # api_version: 2024-02-15-preview\n",
    "    # audience: \"https://cognitiveservices.azure.com/.default\"\n",
    "    # organization: <organization_id>\n",
    "    # deployment_name: <azure_model_deployment_name>\n",
    "  batch_size: 9  # 减小批处理大小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要修改的地方有：\n",
    "\n",
    "llm:\n",
    "1 api_base: http://localhost:8200/v1\n",
    "2 model: /root/autodl-tmp/Qwen/Qwen3-8B (要和vllm服务端的模型名字一致 server-name)\n",
    "3 model_supports_json: false # recommended if this is available for your model.\n",
    "\n",
    "embeddings:\n",
    "1 api_base: http://localhost:8300/v1\n",
    "2 model: gpt-4 (和fastchat那边--model-names名字一致)\n",
    "3 batch_size: 9  # 减小批处理大小 （注意和llm齐平）\n",
    "\n",
    "chunks:\n",
    "1 size: 200（文档不大的时候就小一点）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "graphrag index --root ./openl"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "需要把 vllm服务中的--max_model_len长度增大"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "graphrag query --root ./openl --method local --query  \"请帮我们介绍运动金字塔的内容\" "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ame",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
